/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-24 09:42:14
 * Copyright (c) 2015- joysuch-版权所有
 */
package com.joysuch.wwyt.bp.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.bp.bean.BpTaskListEditBean;
import com.joysuch.wwyt.bp.bean.BpTaskListListBean;
import com.joysuch.wwyt.bp.service.BpTaskListService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.ArrayList;

import static com.joysuch.wwyt.bp.bean.BpTaskListListBean.Search;

@RestController
@RequestMapping("/bp/task-list")
public class BpTaskListController {

    private static final Logger logger = LoggerFactory.getLogger(BpTaskListController.class);

    @Autowired
    private BpTaskListService bpTaskListService;

    @RequestMapping("add")
    public Object add(@Valid BpTaskListEditBean entity) {
        try {
            bpTaskListService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @RequestMapping("update")
    public Object update(@Valid BpTaskListEditBean entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpTaskListService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page, Search condition) {
        try {
            // TODO: 2020-11-25  暂时不提供首页的待办事项 
//            Page<BpTaskListListBean> data = bpTaskListService.findByPage(page,condition);
//            return ResultBean.pageData(data.getContent(), data.getTotalElements());
            return ResultBean.success(new ArrayList<>());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpTaskListService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        try {
            return ResultBean.success(bpTaskListService.getById(id));
        } catch (Exception e) {
            logger.error("查询详情失败", e);
            return ResultBean.fail(101, "查询详情失败:" + e.getMessage());
        }
    }


}
